﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace QiAdmin.Core.Inject
{
    public static class AppService
    {
        public static void AddAppService(this IServiceCollection services, params string[] assemblyNames)
        {
            foreach (var name in assemblyNames)
            {
                Assembly assembly = Assembly.Load(name);
                foreach (var type in assembly.GetTypes())
                {
                    var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
                    if (serviceAttribute != null)
                    {
                        var serviceType = serviceAttribute.ServiceType;
                        switch (serviceAttribute.ServiceLifeType)
                        {
                            case ServiceLifeType.Singleton:
                                services.AddSingleton(serviceType, type);
                                break;
                            case ServiceLifeType.Scoped:
                                services.AddScoped(serviceType, type);
                                break;
                            case ServiceLifeType.Transient:
                                services.AddTransient(serviceType, type);
                                break;
                            default:
                                services.AddTransient(serviceType, type);
                                break;
                        }
                    }
                }
            }
        }
    }
}
